﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Words;
using Aspose.Words.Layout;
using ToolUtity;
namespace AHCS
{
   public class AsposeHelper
    {
        /// <summary>
        /// 拆分doc文件
        /// </summary>
        /// <param name="src"></param>
        public static void Splidoc(string src)
        {
          string outdir=   DirectoryUtil.CreateDirByFile(src);

            Document doc = new Document(src);
            
            //LayoutCollector layoutCollector = new LayoutCollector(doc);
            //doc.UpdatePageLayout();
            //DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);

            DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

            List<int>  intzyft=new List<int>();
            List<int>  intqlyq=new List<int>();
            List<int>  intsms=new List<int>();
            List<int>  intsmsft=new List<int>();
            for (int page = 1; page <= doc.PageCount; page++)
            {
                //Document newDoc = splitter.GetDocumentOfPageRange(3, 5);
                //newDoc.Save(MyDir + “Out.docx”);

                Document pageDoc = splitter.GetDocumentOfPage(page);
                DocumentBuilder builder = new DocumentBuilder(pageDoc);
                string str = pageDoc.Range.Text.Trim() ;
                
                   
                    int subleng = str.Length < 5 ? str.Length : 5;
                    string temstr = str.Substring(0, subleng);
                    if (temstr.IndexOf("摘要附图") >= 0)
                    {
                        intzyft.Add(page);
                    }
                    else if (temstr.IndexOf("权利要求书") >= 0)
                    {
                        intqlyq.Add(page);

                    }
                    else if (temstr.IndexOf("说明书附图") >= 0)
                    {
                        intsmsft.Add(page);
                    }
                    else if (temstr.IndexOf("说明书摘要") >= 0)
                    {
                        
                    }
                    else if (temstr.IndexOf("说明书") >= 0)
                    {
                        intsms.Add(page);
                    }
                   
                
               

               // pageDoc.Save($"sxpliteed_{page}.doc");
            }
            Document newDoc = splitter.GetDocumentOfPage(1);
            newDoc.Save(Path.Combine(outdir, "说明书摘要.rtf"));
         

            if (intzyft.Count > 0)
            {
                Document newDoc2 = splitter.GetDocumentOfPageRange(intzyft[0], intzyft[intzyft.Count - 1]);
                newDoc2.Save(Path.Combine(outdir, "摘要附图.rtf"));
            }
            if (intqlyq.Count > 0)
            {
                Document newDoc3 = splitter.GetDocumentOfPageRange(intqlyq[0], intqlyq[intqlyq.Count - 1]);
                newDoc3.Save(Path.Combine(outdir, "权利要求书.rtf"));
            }
            if (intsms.Count > 0)
            {
                Document newDoc4 = splitter.GetDocumentOfPageRange(intsms[0], intsms[intsms.Count - 1]);
                newDoc4.Save(Path.Combine(outdir, "说明书.rtf"));
            }
            if (intsmsft.Count>0)
            {
                Document newDoc5 = splitter.GetDocumentOfPageRange(intsmsft[0], doc.PageCount);
                newDoc5.Save(Path.Combine(outdir, "说明书附图.rtf"));
            }
          
            
        }

        /// <summary>
        /// 合并文件
        /// </summary>
        /// <param name="src"></param>
        public static void Combiedoc(string f1,string f2,string f3,string f4,string f5,string outdoc)
        {
            Document doc1 = new Document(f1);
            Document doc2 = new Document(f2);
            Document doc3 = new Document(f3);
            Document doc4 = new Document(f4);
            Document doc5 = new Document(f5);

            // Merge documents keeping source file's formatting
            doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
            doc1.AppendDocument(doc3, ImportFormatMode.KeepSourceFormatting);
            doc1.AppendDocument(doc4, ImportFormatMode.KeepSourceFormatting);
            doc1.AppendDocument(doc5, ImportFormatMode.KeepSourceFormatting);

            // Save merged document as DOCX file
            doc1.Save(outdoc, SaveFormat.Doc);


        }

    }
}
